# your ffmpeg road
FFMPEG = ffmpeg

# 定义源目录和目标目录
SRC_DIR = src
MP3_TARGET_DIR = mp3obj
HLS_TARGET_DIR = hlsobj

FLAC_SOURCES = $(wildcard $(SRC_DIR)/*.flac)
WAV_SOURCES = $(wildcard $(SRC_DIR)/*.wav)

MP3_TARGETS_FLAC = $(patsubst $(SRC_DIR)/%.flac,$(MP3_TARGET_DIR)/%.mp3,$(FLAC_SOURCES))
MP3_TARGETS_WAV = $(patsubst $(SRC_DIR)/%.wav,$(MP3_TARGET_DIR)/%.mp3,$(WAV_SOURCES))

MP3_TARGETS = $(MP3_TARGETS_FLAC) $(MP3_TARGETS_WAV)
HLS_TARGETS = $(patsubst $(MP3_TARGET_DIR)/%.mp3,$(HLS_TARGET_DIR)/%.m3u8,$(MP3_TARGETS))

# 默认目标是转换所有文件
all: $(HLS_TARGETS)

$(HLS_TARGETS): $(MP3_TARGETS)
$(HLS_TARGET_DIR)/%.m3u8: $(MP3_TARGET_DIR)/%.mp3
	@mkdir -p $(HLS_TARGET_DIR)
	@mkdir -p $(dir $@)$(basename $(notdir $@))
	$(FFMPEG) -i $< -c:a aac -b:a 192k -f hls -hls_time 10 -hls_list_size 0 -hls_playlist_type vod -hls_segment_filename "$(dir $@)$(basename $(notdir $@))/segment%03d.m4s" $@
	cp $@ $(dir $@)$(basename $(notdir $@))/
	

# 规则：将 .flac 转换为 .mp3
$(MP3_TARGET_DIR)/%.mp3: $(SRC_DIR)/%.flac
	@mkdir -p $(MP3_TARGET_DIR)
	$(FFMPEG) -i $< -c:a libmp3lame -q:a 2 $@

# 规则：将 .wav 转换为 .mp3
$(MP3_TARGET_DIR)/%.mp3: $(SRC_DIR)/%.wav
	@mkdir -p $(MP3_TARGET_DIR)
	$(FFMPEG) -i $< -c:a libmp3lame -q:a 2 $@

# 清理目标文件
.PHONY: clean
clean:
	rm -f $(MP3_TARGET_DIR)/*.mp3
	rm -rf $(HLS_TARGET_DIR)/*
